package controller

import (
	"github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo/v4"
	"github.com/labstack/gommon/log"
	"law_case_api/dto"
	"law_case_api/models"
	"law_case_api/services"
	"law_case_api/utils"
	"net/http"
	"strconv"
	"time"
)

// RoleList
// @Summary 获取当前案件条件的信息
// @Tags 非诉讼案件管理
// @Accept plain
// @Produce plain
// @Param CaseList body dto.CaseListReq true " "
// @Success 200 {object} models.BaseResult
// @Failure 400 {object} models.BaseResult
// @Router /wzj/system/role/list [GET]
func RoleList(c echo.Context) error {
	resp := models.BaseResult{
		Code: 200,
	}
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(*models.JwtCustomClaims)
	userid, _ := strconv.Atoi(claims.UserId)
	if userid == 0 {
		resp.Code = 400
		resp.Message = "用户信息不存在，请重新登录"
		return c.JSON(http.StatusOK, resp)
	}
	isLaw := false
	LoginType := c.Request().Header["Logintype"]
	if len(LoginType) > 0 {
		if LoginType[0] == "1" {
			isLaw = true
		}
	}
	caseListReq := dto.CaseListReq{}
	if err := c.Bind(&caseListReq); err != nil {
		resp.Code = 400
		resp.Message = err.Error()
		return c.JSON(http.StatusBadRequest, resp)
	}
	if caseListReq.PageIndex == 0 {
		caseListReq.PageIndex = 1
	}
	if caseListReq.PageSize == 0 {
		caseListReq.PageSize = 10
	}
	_case := services.CaseService{}
	respData, respPageCount, err := _case.GetCaseListByCondition(userid, isLaw, caseListReq)
	if err != nil {
		resp.Code = 400
		return c.JSON(http.StatusOK, resp)
	}
	resp.PageCount = respPageCount
	var data []dto.CaseInfoReq
	utils.MapTo(respData, &data)
	resp.Data = data

	return c.JSON(http.StatusOK, resp)
}

// UpdateRoleRoute
// @Summary 创建案件信息
// @Tags 非诉讼案件管理
// @Accept plain
// @Produce plain
// @Param CreateCase body dto.CaseInfoReq true " "
// @Success 200 {object} models.BaseResult
// @Failure 400 {object} models.BaseResult
// @Router /wzj/case/create [post]
func UpdateRoleRoute(c echo.Context) error {
	resp := models.BaseResult{
		Code: 200,
	}
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(*models.JwtCustomClaims)
	userid, _ := strconv.Atoi(claims.UserId)
	if userid == 0 {
		resp.Code = 400
		resp.Message = "用户信息不存在，请重新登录"
		return c.JSON(http.StatusOK, resp)
	}
	isLaw := false
	LoginType := c.Request().Header["Logintype"]
	if len(LoginType) > 0 {
		if LoginType[0] == "1" {
			isLaw = true
		}
	}
	caseInfoReq := dto.CaseInfoReq{}
	if err := c.Bind(&caseInfoReq); err != nil {
		resp.Code = 400
		resp.Message = err.Error()
		return c.JSON(http.StatusOK, resp)
	}
	if caseInfoReq.CaseCategory == 0 {
		resp.Code = 400
		resp.Message = "案件类型不能为空"
		return c.JSON(http.StatusOK, resp)
	}
	wzjCaseInfo := models.WzjCaseInfo{}
	wzjCaseDetail := models.WzjCaseDetail{}
	_ = utils.MapTo(caseInfoReq, &wzjCaseInfo)
	_ = utils.MapTo(caseInfoReq, &wzjCaseDetail)
	if isLaw {
		wzjCaseInfo.IsSelfBuild = 0
	}
	//调用案件服务
	_case := services.CaseService{}
	if wzjCaseInfo.Id > 0 {
		boolRes, err := _case.UpdateCaseInfo(wzjCaseInfo, wzjCaseDetail)
		if err == nil && boolRes {
			resp.Message = "更新案件成功"
			return c.JSON(http.StatusOK, resp)
		}
		log.Error("更新案件失败：", err) //增加日志信息
		resp.Code = 400
		resp.Message = "更新案件失败"
		return c.JSON(http.StatusOK, resp)
	} else {
		wzjCaseInfo.CaseCode = utils.GenerateSpecialCode("YK")
		wzjCaseInfo.CreateDate = time.Now().Unix()
		wzjCaseInfo.CaseCreatedDate = time.Now().Unix() * 1000
		wzjCaseInfo.CreateUserId = int64(userid)
		boolRes, err := _case.CreateCaseInfo(wzjCaseInfo, wzjCaseDetail)
		if err == nil && boolRes {
			resp.Message = "创建案件成功"
			return c.JSON(http.StatusOK, resp)
		}
		log.Error("创建案件失败：", err) //增加日志信息
		resp.Code = 400
		resp.Message = "创建案件失败"
		return c.JSON(http.StatusOK, resp)
	}
}
